package com.gxuwz.leave.servlet;

import com.gxuwz.leave.context.AppContext;
import com.gxuwz.leave.entity.Class;
import com.gxuwz.leave.entity.Course;
import com.gxuwz.leave.entity.Student;
import com.gxuwz.leave.service.ClassService;
import com.gxuwz.leave.service.CourseService;
import com.gxuwz.leave.service.StuService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "stuServlet", urlPatterns = {"/stu"})
public class StuServlet extends BaseHttpServlet{
    private final static String VIEW_ADD = "/WEB-INF/views/student/stu_add.jsp";

    private final static String VIEW_EDIT = "/WEB-INF/views/student/stu_edit.jsp";

    private final static String VIEW_LIST = "/WEB-INF/views/student/stu_list.jsp";

    private final static String ACTION_LIST = "/stu?action=list";

    private StuService stuService;
    private ClassService classService;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        String view = ErrorPage_500;
        try{
            switch (action){
                //导向新增页面
                case "input":
                    view = VIEW_ADD;
                    Student stu = new Student();
                    List<Class> classList = classService.list(null);
                    req.setAttribute("stu", stu);
                    req.setAttribute("classList", classList);
                    break;
                //提交新增
                case "add":
                    stu = new Student();
                    stu.setStuID(req.getParameter("stuID"));
                    stu.setClassID(req.getParameter("classID"));
                    stu.setPassword(req.getParameter("password"));
                    stu.setStuName(req.getParameter("stuName"));
                    stu.setSex(req.getParameter("sex"));
                    stu.setAddress(req.getParameter("address"));
                    stu.setStuTel(req.getParameter("stuTel"));
                    stu.setContact(req.getParameter("contact"));
                    stu.setContactTel(req.getParameter("contactTel"));
                    stuService.addStu(stu);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //编辑操作时，获取那一行的数据
                case "get":
                    String stuID = req.getParameter("stuID");
                    stu = stuService.findStu(stuID);
                    classList = classService.list(null);
                    view = VIEW_EDIT;
                    req.setAttribute("stu", stu);
                    req.setAttribute("classList", classList);
                    break;
                //提交编辑操作
                case "edit":
                    stu = new Student();
                    stu.setStuID(req.getParameter("stuID"));
                    stu.setClassID(req.getParameter("classID"));
                    stu.setPassword(req.getParameter("password"));
                    stu.setStuName(req.getParameter("stuName"));
                    stu.setSex(req.getParameter("sex"));
                    stu.setAddress(req.getParameter("address"));
                    stu.setStuTel(req.getParameter("stuTel"));
                    stu.setContact(req.getParameter("contact"));
                    stu.setContactTel(req.getParameter("contactTel"));
                    stuService.editStu(stu);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                //搜索
                case "search":
                    String keywords = req.getParameter("keywords");
                    stu = new Student();
                    stu.setStuID(keywords);
                    List<Student> list = stuService.list(stu);
                    if(list.isEmpty()){
                        stu = new Student();
                        stu.setStuName(keywords);
                        list = stuService.list(stu);
                    }
                    view = VIEW_LIST;
                    req.setAttribute("list", list);
                    break;
                //删除操作
                case "delete":
                    stuID = req.getParameter("stuID");
                    stuService.deleteStu(stuID);
                    req.setAttribute("operation", "success");
                    view = ACTION_LIST;
                    break;
                case "list":
                    List<Student> studentList = stuService.list(null);
                    view = VIEW_LIST;
                    req.setAttribute("list", studentList);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            req.setAttribute("message", e.getMessage());
            req.setAttribute("operation", "failure");
            view = ACTION_LIST;
        }
        super.doDispatch(req,resp,view);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        stuService = AppContext.getBean(StuService.class);
        classService = AppContext.getBean(ClassService.class);
    }
}
